package com.fxz;

import com.fxz.bean.factory.support.DefaultListableBeanFactory;
import com.fxz.bean.resource.ClassPathResource;
import com.fxz.bean.xml.XmlBeanDefinitionReader;
import com.fxz.pojo.Role;
import com.fxz.pojo.User;

import java.io.InputStream;

/**
 * @author Fxz
 * @version 1.0
 * @date 2021-08-12 20:59
 */
public class Test {

    public static void main(String[] args) {
        //本地配置文件
        String location = "applicationContext.xml";
        //创建bean工厂 拥有自动装配 管理bean 管理单实例bean的能力
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        //创建读取xml的类
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        ClassPathResource classPathResource = new ClassPathResource(location);
        //将xml转为流
        InputStream inputStream = classPathResource.getInputStream();
        //将流解析并且将解析出来的BeanDefinition缓存
        beanDefinitionReader.loadBeanDefinitions(inputStream);
        //先看缓存中是否存在 不存在则根据BeanDefinition创建bean
        Role role = (Role) beanFactory.getBean("role");
        System.out.println(role);
        User user = (User) beanFactory.getBean("user");
        System.out.println(user);
        role = (Role) beanFactory.getBean(Role.class);
        System.out.println(role);
        user = (User) beanFactory.getBean(User.class);
        System.out.println(user);
    }
}
